﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Locator.Silverlight.Client.Models.InventoryServiceReference;
using System.Collections.Generic;
using Locator.Silverlight.Client.Models.ViewModels.Elements;

namespace Locator.Silverlight.Client.Models
{
    public class InventoryModel : ModelBase<IInventoryServiceAsync>, IInventoryModel
    {
        /// <summary>
        /// Initializes a new instance of the InventoryModel class.
        /// </summary>
        public InventoryModel()
            : base(new InventoryServiceClient("BasicHttpBinding_IInventoryService"))
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the InventoryModel class.
        /// </summary>
        /// <param name="inventoryService"></param>
        public InventoryModel(IInventoryServiceAsync inventoryService)
            : base(inventoryService)
        {
        }

        public void GetInventoriesAsync(InventoryRequestHelper request, Action<IEnumerable<StoreWithInventoryViewModel>, Exception> callback)
        {
            this.Service.GetInventoriesWithRequestCompleted += (sender, eventArgs) => 
            {
                if (eventArgs.Error != null)
                {
                    callback(null, eventArgs.Error);
                }
                else
                {
                    var storesWithInventoriesViewModelList = new List<StoreWithInventoryViewModel>();
                    foreach (var storeWithInventories in eventArgs.Result)
                    {
                        storesWithInventoriesViewModelList.Add(new StoreWithInventoryViewModel(storeWithInventories));
                    }

                    callback(storesWithInventoriesViewModelList, null);
                }

            };

            this.Service.GetInventoriesWithRequestAsync(request.Request);

            //this.Service.BeginGetInventoriesWithRequest(request.Request, asyncState =>
            //{
            //    try
            //    {
            //        var inventoryCollection = this.Service.EndGetInventoriesWithRequest(asyncState);
            //        var storesWithInventoriesViewModelList = new List<StoreWithInventoryViewModel>();
            //        foreach (var storeWithInventories in inventoryCollection)
            //        {
            //            storesWithInventoriesViewModelList.Add(new StoreWithInventoryViewModel(storeWithInventories));
            //        }

            //        callback(storesWithInventoriesViewModelList, null);
            //    }
            //    catch (Exception ex)
            //    {
            //        callback(null, ex);
            //    }
            //}, null);
        }

        public void GetProductsForUserAsync(Action<IEnumerable<ProductViewModel>, Exception> callback)
        {
            this.Service.GetProductsForCurrentUserCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error != null)
                {
                    callback(null, eventArgs.Error);
                }
                else
                {
                    var productViewModelList = new List<ProductViewModel>();
                    foreach (var product in eventArgs.Result)
                    {
                        productViewModelList.Add(new ProductViewModel() { Id = product.Id, Name = product.Name });
                    }

                    callback(productViewModelList, null);
                }

            };

            this.Service.GetProductsForCurrentUserAsync();

            //this.Service.BeginGetProductsForCurrentUser(asyncState =>
            //{
            //    try
            //    {
            //        var productCollection = this.Service.EndGetProductsForCurrentUser(asyncState);
            //        var productViewModelList = new List<ProductViewModel>();
            //        foreach (var product in productCollection)
            //        {
            //            productViewModelList.Add(new ProductViewModel(product));
            //        }

            //        callback(productViewModelList, null);
            //    }
            //    catch (Exception ex)
            //    {
            //        callback(null, ex);
            //    }
            //}, null);
        }

        public void GetStoresForUserAsync(Action<IEnumerable<StoreViewModel>, Exception> callback)
        {
            this.Service.GetStoresForCurrentUserCompleted += (sender, eventArgs) =>
            {
                if (eventArgs.Error != null)
                {
                    callback(null, eventArgs.Error);
                }
                else
                {
                    var storeViewModelList = new List<StoreViewModel>();
                    foreach (var store in eventArgs.Result)
                    {
                        StoreViewModel newStoreViewModel = new StoreViewModel()
                        {
                            Id = store.Id,
                            Latitude = store.Latitude,
                            Longitude = store.Longitude
                        };
                        newStoreViewModel.SetAddress(store.Address, store.City);
                        storeViewModelList.Add(newStoreViewModel);
                    }

                    callback(storeViewModelList, null);
                }

            };

            this.Service.GetStoresForCurrentUserAsync();

            //this.Service.BeginGetStoresForCurrentUser(asyncState =>
            //{
            //    try
            //    {
            //        var storeCollection = this.Service.EndGetStoresForCurrentUser(asyncState);
            //        var storeViewModelList = new List<StoreViewModel>();
            //        foreach (var store in storeCollection)
            //        {
            //            StoreViewModel newStoreViewModel = new StoreViewModel() { Id = store.Id, 
            //                                                                        Latitude = store.Latitude, 
            //                                                                        Longitude = store.Longitude };
            //            newStoreViewModel.SetAddress(store.City, store.Address);
            //            storeViewModelList.Add(newStoreViewModel);
            //        }

            //        callback(storeViewModelList, null);
            //    }
            //    catch (Exception ex)
            //    {
            //        callback(null, ex);
            //    }
            //}, null);
        }
    }
}
